package com.google.code.mochaccino.framework.ui.demo.pages;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.ui.services.ServiceResult;
import java.util.List;
import java.util.Locale;
import org.codehaus.jackson.annotate.JsonPropertyOrder;

/**
 *
 */
public interface UserSettingsService {

	/** Return the Available Locales */
	List<Locale> getAvailableLocale();

	/** Return the maximum console timeout in minutes */
	int getMaximumConsoleTimeout();

	/** Return the minimum console timeout */
	int getMinimumConsoleTimeout();

	/** Get the user info */
	UserInfo getUserInfo( String userId );

	/** Save the user info */
	ServiceResult saveUserInfo( UserInfo info, Locale locale );

	@JsonPropertyOrder( { "username",
	                      "firstname",
	                      "lastname",
	                      "email",
	                      "language",
	                      "timeout" } )
	public static class UserInfo implements Comparable<UserInfo> {

		private Integer timeout   = null;
		private Integer version   = null;
		private Long    id        = null;
		private String  email     = null;
		private String  firstname = null;
		private String  language  = null;
		private String  lastname  = null;
		private String  username  = null;

		public UserInfo() {
		}

		public UserInfo( long id, int version, String username, String firstname, String lastname, String email, String language, int timeout ) {
			this.id = id;
			this.version = version;
			this.email = email;
			this.firstname = firstname;
			this.language = language;
			this.lastname = lastname;
			this.username = username;
			this.timeout = timeout;
		}

		public UserInfo( long id, int version, String username, String firstname, String lastname, String email, Locale language, int timeout ) {
			this.id = id;
			this.version = version;
			this.email = email;
			this.firstname = firstname;
			this.language = language.toString();
			this.lastname = lastname;
			this.username = username;
			this.timeout = timeout;
		}

		@Override
		public boolean equals( Object o ) {
			if ( o instanceof UserInfo ) {
				return this.compareTo( (UserInfo) o ) == 0;
			}

			return super.equals( o );
		}

		public int compareTo( UserInfo userInfo ) {
			int result = this.username.compareTo( userInfo.username );
			if ( result != 0 ) {
				return result;
			}

			if ( this.firstname != null && userInfo.firstname != null ) {
				result = this.firstname.compareTo( userInfo.firstname );
				if ( result != 0 ) {
					return result;
				}
			} else if ( this.firstname != null && userInfo.firstname == null ) {
				return -1;
			} else if ( this.firstname == null && userInfo.firstname != null ) {
				return 1;
			}

			if ( this.lastname != null && userInfo.lastname != null ) {
				result = this.lastname.compareTo( userInfo.lastname );
				if ( result != 0 ) {
					return result;
				}
			} else if ( this.lastname != null && userInfo.lastname == null ) {
				return -1;
			} else if ( this.lastname == null && userInfo.lastname != null ) {
				return 1;
			}

			if ( this.language != null && userInfo.language != null ) {
				result = this.language.compareTo( userInfo.language );
				if ( result != 0 ) {
					return result;
				}
			} else if ( this.language != null && userInfo.language == null ) {
				return -1;
			} else if ( this.language == null && userInfo.language != null ) {
				return 1;
			}

			if ( this.email != null && userInfo.email != null ) {
				result = this.email.compareTo( userInfo.email );
				if ( result != 0 ) {
					return result;
				}
			} else if ( this.email != null && userInfo.email == null ) {
				return -1;
			} else if ( this.email == null && userInfo.email != null ) {
				return 1;
			}

			if ( this.timeout < userInfo.timeout ) {
				return -1;
			}

			if ( this.timeout > userInfo.timeout ) {
				return 1;
			}
			return 0;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail( String email ) {
			this.email = email;
		}

		public String getFirstname() {
			return firstname;
		}

		public void setFirstname( String firstname ) {
			this.firstname = firstname;
		}

		public Long getId() {
			return id;
		}

		public String getLanguage() {
			return language;
		}

		public void setLanguage( String language ) {
			this.language = language;
		}

		public String getLastname() {
			return lastname;
		}

		public void setLastname( String lastname ) {
			this.lastname = lastname;
		}

		public Integer getTimeout() {
			return timeout;
		}

		public void setTimeout( Integer timeout ) {
			this.timeout = timeout;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername( String username ) {
			this.username = username;
		}

		public Integer getVersion() {
			return version;
		}

		@Override
		public int hashCode() {
			if ( username == null ) {
				return super.hashCode();
			}

			return username.hashCode();
		}
	}
}
